// pages/customer/my.js
const app = getApp();
const util = require("../../utils/util.js");
const network = require("../../utils/network");
const customerConfig = require("./config/customerConfig");

Page({
    onReady() {
        this.getIndustryList()
    },
    /**
     * 页面的初始数据
     */
    data: {
        searchForm: {
            name: null,
            industry_id: null,
            industry_name: null,
        },
        industryPopShow: false,
        industry_arr: [],
        curTabListData: [[], []],
        fabs: [{
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145321081195.png',
            text: '搜索'
        }, {
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145517219958.png',
            text: '刷新'
        }],
        tabs: customerConfig.defaultTabs(app.api.customerMy, app.api.customerMy)
    },

    updateTabListData({detail: {index, list: {id, name, list, listParams}}}) {
        const key = 'curTabListData[' + index + ']'
        this.setData({
            [key]: list
        }, function () {
            util.hideLoading()
        })
    },

    toPage_left1({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/customer/follow?id=' + id
        });
    },

    toPage_left2({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/customer/detail?id=' + id
        });
    },

    toPage_right1({detail: {id}}) {
        this.selectComponent('.confirm-dialog-del').show({id});
    },

    toPage_right2({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/customer/detail?id=' + id
        });
    },

    onConfirmDel({detail: {id}}) {
        // 放弃跟进
        util.showLoading('操作中')
        network.reqPromise('POST', {
            uri: app.api.relieve + '?customer_id=' + id + '&type=2',
        }).then(res => {
            this.selectComponent('.tabListComponent').handlePullDownRefresh()
            util.hideLoading()
        }).catch(() => {
            util.showToast('解除失败')
            util.hideLoading()
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.selectComponent('.tabListComponent').handlePullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.selectComponent('.tabListComponent').handleReachBottom()
    },
    search() {
        const key = 'tabs[' + this.selectComponent('.tabListComponent').data.activeTabIdx + '].request.searchParams'
        this.setData({
            [key]: this.data.searchForm
        })
        this.selectComponent('.tabListComponent').handlePullDownRefresh()
    },
    reset() {
        this.setData({
            searchForm: {
                name: '',
                industry_id: '',
                industry_name: ''
            }
        })
        const key = 'tabs[' + this.selectComponent('.tabListComponent').data.activeTabIdx + '].request.searchParams'
        this.setData({
            [key]: this.data.searchForm
        })
        this.selectComponent('.tabListComponent').handlePullDownRefresh()
    },
    showSearchBox() {
        this.setData({
            searchForm: this.data.tabs[this.selectComponent('.tabListComponent').data.activeTabIdx].request.searchParams
        })
    },
    inputChange(e) {
        this.setData({
            [e.target.dataset.key]: e.detail
        })
    },
    onConfirmIndustry(e) {
        this.setData({
            'searchForm.industry_id': e.detail.value.id,
            'searchForm.industry_name': e.detail.value.name,
            industryPopShow: false
        });
    },
    onCancelIndustry() {
        this.setData({
            industryPopShow: false
        });
    },
    getIndustryList() {
        util.showLoading('加载中')
        const that = this
        network.GET({
            uri: app.api.industryList,
            success(res) {
                that.setData({
                    industry_arr: res.data
                })
                util.hideLoading()
            },
            fail() {
                util.hideLoading()
            }
        })
    },
    // 显示行业弹出层
    showIndustryPop() {
        this.setData({
            industryPopShow: true
        })
    },
    clickFab({
                 detail
             }) {
        const _this = this
        if (detail === '刷新') {
            _this.selectComponent('.tabListComponent').handlePullDownRefresh()
        }
        if (detail === '搜索') {
            _this.selectComponent('.search-box').show();
        }
    },
})
